package com.caption.utils;


import com.caption.config.ApplicationContextRegister;
import org.springframework.context.ApplicationContext;


/**
 * spring获取对象工具类
 */
public class ApplicationUtils {
    private final static ApplicationContext applicationContext =
            ApplicationContextRegister.getApplicationContext();

    public ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    public static <T> T getBean(String beanName){
      if(containBean(beanName)){
         Class<T> type = getTpye(beanName);
         return applicationContext.getBean(beanName,type);
      }
      return null;
    }

    public static <T> T getBean(String beanName,Class<T> requiredType){
        if(containBean(beanName)){
            return applicationContext.getBean(beanName,requiredType);
        }
        return null;
    }
    public static <T>Class<T> getTpye(String name){

        return (Class<T>) applicationContext.getType(name);
    }
    public static boolean containBean(String beanName){
      return applicationContext.containsBean(beanName);
    }

    public static boolean isSingleton(String beanName){
        return applicationContext.isSingleton(beanName);
    }

}
